/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function dialogNewComponent() {
    $.ajax({
        "url":'a_new_component',
        "type":'GET',
        "success":function(htmlData) {
            $.Dialog({
                "title": "Свойства оборудования",
                "draggable": true,
                "content": htmlData,
                "onShow" : function(){
                    loadMainOptionTree();
                    setDatepickerDefDate('dnc_buy_date');
                },
                "width" : 750
            });
        }
    });
}

function loadTypeJSON() {
    var json;
    $.ajax({
       "url":'a_get_jscomponents',
       "type":'GET',
       "dataType":'json',
       "async":false,
       "success": function(resp) {
           json = resp;
       }
    });
    return json;
}

function  newComponentNext() {
    var string = $('#newComponentFormData').serialize();
    console.log(string);
    $.ajax({
        "url":'a_submit_component_data',
        "type":'POST',
        "data":string,
        "success":function(data) {
            var err = $(data).find('#dnc_validation_error').val();
                if (err === 'false') {
                    $.Dialog.close();
                    $.Dialog({
                        "title": "Свойства оборудования",
                        "draggable": true,
                        "content": data,
                        "width" : 600,
                        "onShow" : function() {
                            
                            loadSeconsOptionTree();
                            
                            $('#dnc_select_computer')[0].disabled = true;
                            $('#dnc_select_employee')[0].disabled = true;
                        }
                    });
                    
                } else {
                    $('div.window div.content')[0].innerHTML = data;
                    loadMainOptionTree();
                }
        }
    });
}

function loadMainOptionTree() {
    var options = {select_class: 'vertical', "choose" : '...', "empty_value": -1};
    var json = loadTypeJSON();
    $('#selectType').optionTree(json,options);
}

function loadSeconsOptionTree() {
    var options = {"select_class": 'vertical blockable', "choose" : '...', "empty_value": -1};
    var json = loadTypeJSON();
    $('input[name=dnc_computer_type]').optionTree(json,options).change(typeChanged);
    $('.blockable').prop('disabled',true);
}

function typeChanged() {
    var typeId = $('#dnc_computer_type').val();
    if (typeId != undefined && typeId !== '-1') {
        $.ajax({
            "url":'a_get_components_by_type',
            "dataType":'json',
            "data":{"typeId":typeId},
            "success":function (json) {
                document.getElementById('dnc_computer_id').value = -1;
                var select = document.getElementById('dnc_select_computer');
                select.innerHtml = '<option value="-1">Выберите устройство</option>';
                    for(var i=0;i<json.data.length;i++) {
                        var option = document.createElement("option");
                        option.text = json.data[i].name;
                        option.value = json.data[i].id;
                        select.appendChild(option);
                    }
            }
        });
    }
}

function submitNewComponent() {
    var string = $('#newComponentFormRelation').serialize();
    $.ajax({
        "url":'a_submit_component',
        "type":'POST',
        "data":string,
        "success":function(data) {
            var err = $(data).find('#dnc_validation_error').val();
            console.log(err);
                if (err === 'false') {
                    dialogSuccessfulClose('Оборудование зачислено.');
                    redrawDataTable('component_table');
                } else {
                    $('div.window div.content')[0].innerHTML = data;
                }
        }
    });
}

function officeChanged() {
    var select = document.getElementById('dnc_select_office');
    document.getElementById('dnc_officeId').value = select.value;
    var officeId = select.value;
    if (officeId !== '-1') {
        select.options[0].disabled = true;
    }
}

function employeeChanged() {
   var emplSelect = document.getElementById('dnc_select_employee');
   document.getElementById('dnc_employee_id').value = emplSelect.value;
}

function computerChanged() {
   var compSelect = document.getElementById('dnc_select_computer');
   document.getElementById('dnc_computer_id').value = compSelect.value;
}

function dncChecked(element) {
    switch(element.id) {
        case 'dnc_employee_check': 
            if (element.checked) {
                resetComputerPanel();
                $('#dnc_select_employee')[0].disabled = false;
            } else resetEmployeePanel();
            break;
        case 'dnc_computer_check': 
            if (element.checked) {
                resetEmployeePanel();
                $('.blockable').prop('disabled',false);
                $('#dnc_select_computer')[0].disabled = false;
            } else resetComputerPanel();
            break;
    }
}

function resetEmployeePanel() {
    $('#dnc_employee_check')[0].checked = false;
    $('#dnc_employee_id').val(0);
    $('#dnc_select_employee')[0].disabled = true;
}

function resetComputerPanel() {
    $('#dnc_computer_check')[0].checked = false;
    $('#dnc_computer_id').val(0);
    $('.blockable').prop('disabled',true);
    $('#dnc_select_computer')[0].disabled = true;
}